package com.itboye.jinpuyiyuan.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.IllRecordAdapter;
import com.itboye.jinpuyiyuan.adapter.MeHosAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.InHosBean;
import com.itboye.jinpuyiyuan.bean.LaborRecordBean;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/1/10 0010.
 */

public class TestRecordActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private List<LaborRecordBean.Reports.ReportInfo> list;
    private BranchAddressPresenter presenter;
    private String uid;

    private SmartRefreshLayout refresh;
    private RecyclerView inHos;

    private MeHosAdapter meHosAdapter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_ill);


        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("化验记录");
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);
        list=new ArrayList<>();

        meHosAdapter=new MeHosAdapter(this,list,3,3);
        meHosAdapter.setOnItemClick(new MeHosAdapter.OnItemClick() {
            @Override
            public void setOnItemClic(int position) {
                Intent intent = new Intent(TestRecordActivity.this, HospitalDetailActivity.class);
//                report_type=getIntent().getStringExtra("report_type");
//                report_id=getIntent().getStringExtra("report_id");
                intent.putExtra("report_type",list.get(position).getReport_type());
                intent.putExtra("report_id",list.get(position).getReport_id());

                startActivity(intent);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        inHos.setLayoutManager(linearLayoutManager);
        inHos.setAdapter(meHosAdapter);


        refresh.setEnableLoadmore(false);
//        WaveSwipeHeader waveSwipeHeader = new WaveSwipeHeader(this);
//        waveSwipeHeader.setColorSchemeColors(getResources().getColor(R.color.colorAccent));

        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.getLaborRecord(uid);
            }
        });

        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        ClassicsFooter classicsFooter = new ClassicsFooter(this);

        refresh.setRefreshHeader(classicsHeader);
        refresh.setRefreshFooter(classicsFooter);
        showProgressDialog("数据获取中,请稍等...",false);

//        payment_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                startActivity(new Intent(TestRecordActivity.this,TestRecordDetailActivity.class));
//            }
//        });
    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);

        if (entity!=null){
            try{
                refresh.finishRefresh();
                closeProgressDialog();

            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_laboratoryRecord_success){ //处方记录
                LaborRecordBean orderList= (LaborRecordBean) entity.getData();
                List<LaborRecordBean.Reports.ReportInfo> orders = orderList.getReport_list().get(0).getReport_info();

                if (this.list.size()>0){
                    this.list.clear();
                }
                this.list.addAll(orders);
                meHosAdapter.addData(list,3,3);
            }else  if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_laboratoryRecord_fail){
                MAlert.alert(entity.getMsg());

            }
        }
    }
}
